package org.linlinjava.litemall.admin.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallComment;
import org.linlinjava.litemall.db.service.LitemallCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/comment")
@Validated
public class AdminCommentController {
    @Autowired
    LitemallCommentService commentService;


    @RequiresPermissions("admin:comment:list")
    @RequiresPermissionsDesc(menu = {"用户管理", "商品评论"}, button = "查询")
    @GetMapping("/list")
    public Object list(String userId,String valueId,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time")String sort,
                       @Order @RequestParam(defaultValue = "desc")String order){
        List<LitemallComment> litemallComments = commentService.queryByExample(userId,valueId,page,limit,sort,order);
        return ResponseUtil.okList(litemallComments);
    }

    @RequiresPermissions("admin:comment:delete")
    @RequiresPermissionsDesc(menu = {"用户管理", "商品评论"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallComment comment){
        Integer id = comment.getId();
        if(id == null){
            return ResponseUtil.badArgument();
        }
        commentService.deleteById(id);
        return ResponseUtil.ok();
    }
}
